/*
 * @(#) CommonDaoImpl.java 2010-9-2
 *
 * Copyright (c) 2010, Strong Technology Inc. All Rights Reserved.
 * Strong System Inc. CONFIDENTIAL
 */
package cn.strong.gweva.common.integration.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.integration.dao.CommonDao;
import cn.strong.gweva.common.util.SqlBatchUtil;

/**
 * 
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2010-9-2
 */
@SuppressWarnings("unchecked")
public class CommonDaoImpl extends BaseDao implements CommonDao
{
	/**
	 * 查询List
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public List searchList(String searchMethod, Object obj) throws Exception {
		return this.getSqlMapClientTemplate().queryForList(searchMethod, obj);
	}
	
	/**
	 * 查询
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public Object searchObj(String searchMethod, Object obj) throws Exception {
		return this.getSqlMapClientTemplate().queryForObject(searchMethod, obj);
	}
	
	/**
	 * 查询个数
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int searchCount(String searchMethod, Object obj) throws Exception {
		return ((BigDecimal)this.getSqlMapClientTemplate().queryForObject(searchMethod, obj))
				.intValue();
	}
	
	/**
	 * 修改
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int update(String searchMethod, Object obj) throws Exception {
		return this.getSqlMapClientTemplate().update(searchMethod, obj);
	}
	
	/**
	 * 批量修改
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int updateBatch(String searchMethod, List obj) throws Exception {
		return SqlBatchUtil.doUpdateBatch(this, searchMethod, obj);
	}
	
	/**
	 * 删除
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public int delete(String searchMethod, Object obj) throws Exception {
		return this.getSqlMapClientTemplate().delete(searchMethod, obj);
	}
	
	/**
	 * 批量删除
	 * 
	 * @param searchMethod
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public void deleteBatch(String searchMethod, List obj) throws Exception {
		SqlBatchUtil.doDeleteBatch(this,searchMethod, obj);
	}
	
	
	/**
	 * 增加
	 * 
	 * @param searchMethod
	 * @param obj
	 * @throws Exception
	 */
	public Object insert(String searchMethod, Object obj) throws Exception {
		return this.getSqlMapClientTemplate().insert(searchMethod, obj);
	}
	
	/**
	 * 批量增加
	 * 
	 * @param searchMethod
	 * @param obj
	 * @throws Exception
	 */
	public void insertBatch(String searchMethod, List obj) throws Exception {
		SqlBatchUtil.doInsertBatch(this,searchMethod, obj);
	}
	
	/**
	 * 分页检索
	 * @param entity 
	 * @param searchMethod SQL ID
	 * @param obj 检索Entity
	 * @return
	 * @throws Exception
	 */
	public PagerEntity searchPager(PagerEntity entity,String searchMethod, Object obj) throws Exception
	{
		if(entity == null)
		{
			entity = new PagerEntity();
		}
		// 获取每页显示的件数
		//entity.getPageInfo().setPageSize((BigDecimal)this.searchObj(Constants4Method.COMMON_SEARCH_PAGE_SIZE, null));
		if (entity.getPageInfo().getPageSize() == null)
		{
			entity.getPageInfo().setPageSize(new BigDecimal(15));
		}
		
		entity.getPageInfo().setPageCount(new BigDecimal(this.getSqlMapClientTemplate().queryForList(searchMethod, obj).size()));
		
		entity.setResultList(this.getSqlMapClientTemplate().queryForList(searchMethod, obj, entity.getPageInfo().getPageStart().intValue(),
				entity.getPageInfo().getPageSize().intValue()));
		
		return entity;
	}
}

		